/*
Building a Clean and Loyal State: The Strategic Dilemma of an Anti-corruption Campaign Under One-Party Rule
Authors: Tianyang Xi, Yang Yao and Qian Zhang
Date: Oct, 30, 2023
Stata Version 14.0
Operating System: Micrsoft Windows 10
*/

clear
set more off
capture log close

*-------------------------------Figure 4------------------------------------*

use "investigation_2013_2021.dta", clear
drop if ability==. | gender==.
tab connectionc // density of connection

capture program drop abilityMEconnection 

program define abilityMEconnection, rclass
	preserve
	*AME of ability
	gen ability_int=int(ability*100)/100
	hetprobit lm ability_int connectionc jiao1_a xs_2014 xs_2015 xs_2016 xs_2017 xs_2018 xs_2019 xs_2020 xs_2021 college_onjob_yes mini_nat gender year2014 year2015 year2016 year2017 year2018 year2019 year2020 year2021, het(ability jiao1_a_yes connectionc) cluster(location)
	margins, dydx(ability_int)
	matrix E=r(table)
	global abilityME =E[1,1]
	*AME of interaction
	hetprobit lm ability connectionc jiao1_a xs_2014 xs_2015 xs_2016 xs_2017 xs_2018 xs_2019 xs_2020 xs_2021 college_onjob_yes mini_nat gender year2014 year2015 year2016 year2017 year2018 year2019 year2020 year2021, het(ability jiao1_a_yes connectionc) cluster(location)
	margins, dydx(jiao1_a)
	matrix E=r(table)
	global interME=E[1,1]
	forvalues i=0/20{
		*dis `me`i''
		return scalar me`i'= $abilityME + $interME * `i'
	}
	restore
end

bootstrap r(me0) r(me1) r(me2) r(me3) r(me4) r(me5) r(me6) r(me7) r(me8) r(me9) r(me10) r(me11) r(me12) r(me13) r(me14) r(me15) r(me16) r(me17) r(me18) r(me19) r(me20), reps(200) seed(12345) nodots saving("Figure4.dta", replace): abilityMEconnection

*then get marginal_effects_of_ability.xlsx to calculate confidence intervals at 95%

*marginal_effects_of_ability.dta contains main variables from marginal_effects_of_ability.xlsx and density of connection（line 17 in this dofile）.

use "marginal_effects_of_ability.dta", clear
set scheme lean1

#delimit
graph twoway (bar density_c connection, scale(1) yaxis(1) xlabel(0(1)20) ylabel(0(0.1)0.7, axis(1)) ytitle(Density of connection(categorical),tstyle(smbody)))
		(rarea ci_high ci_low connection, color(gs12) yaxis(2) ylabel(-10(1)1, axis(2)) xtitle(Connection (categorical)) ytitle(Marginal effect of Capability on the probability of investigation,tstyle(smbody) axis(2))) 
		(line y0 connection, yaxis(2) )
		(line ame connection,lpattern(solid) color(black) yaxis(2)), 
	   legend(off) ;
#delimit cr
graph save Graph "Figure 4.gph", replace




